// pages/operation/class/classInfoList/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js"
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    beOver: "", // 是否结业  传过来的 1：正常 0：已结业
    allPermissions: [],
    activeTab: "first",
    spotMap: {},
    classId: "",
    courseId: "", // 班级id
    name: "", // 课程名称
    courseName: "", // 关联课程
    idsStr: "",
    teacherName: "", // 班级老师
    classTypeName: "", // 班级分类
    classHour: "", // 授课课时
    isVip: "",
    plannedStudentCount: "",
    studentCount: "",
    courseList: [], // 排课列表
    dateList: [], // 存储日期的数组
    nowDate: "", // 系统当前日期
    studentTableDatas: [], // 班级学员
    orderTableDatas: [], // 点名列表
    currentOrderDate: "", // 当前点名日期

    // 移除本班
    showPicker: false,
    columnDatas: [],
    removeId: "", // 需要移除的学生id
    removeStudentId: "", // 

    // 调整课次
    showSchedule: false,
    scheduleObj: {
      id: "", // 
      cofId: "",
      orderNo: "",
      className: "", // 班级
      classDate: "", // 课程日期
      classStartTime: "", // 上课时间
      classEndTime: "", // 下课时间
      teacherName: "", // 上课老师
      classRoomName: "", // 上课教室
      totalNum: "", // 上课学员
      type: "", // 1: 调整此次课次  2：调整此次及后续课次
    },
    showOrderDetail: false,
    orderDetails: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 接收传过来的id
    this.setData({
      courseId: options.id,
      beOver: options.beOver
    })
    // 班级详情
    // await this.getClassInfo()
    // // 获取排课信息
    // await this.getOrderInfoList()
    // // 获取班级学员
    // await this.getStudentList()
    // // 获取点名日期
    // await this.getOrderDate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      allPermissions: wx.getStorageSync('permissions')
    })
    // 班级详情
    this.getClassInfo()
    // 获取排课信息
    this.getOrderInfoList()
    // 获取班级学员
    this.getStudentList()
    // 获取点名日期
    this.getOrderDate()

    this.getOrdeListByDate(this.data.currentOrderDate)
  },

  async getClassInfo() {
    classModel.getClassInfoApi({ id: this.data.courseId }).then(res => {
      this.setData({
        classId: res.id,
        name: res.name,
        courseName: res.courseName,
        idsStr: res.courseId,
        teacherName: res.teacherName,
        classTypeName: res.classTypeName,
        classHour: res.classHour,
        isVip: res.isVip,
        plannedStudentCount: res.plannedStudentCount,
        studentCount: res.studentCount
      })
    })
  },

  // 获取排课信息
  async getOrderInfoList() {
    classModel.getOrderInfoListApi({ classId: this.data.courseId, pageNum: 1, pageSize: 999 }).then(res => {
      res.rows.forEach(item => {
        item.startTime = item.startTime.split(" ")[0];
        item.endTime = item.endTime.split(" ")[0];
        item.weekName = item.circulateType == "2" ? `每周${{
          1: "一",
          2: "二",
          3: "三",
          4: "四",
          5: "五",
          6: "六",
          7: "日",
        }[item.weekNum]}` : "每天"
      })
      this.setData({
        courseList: res.rows
      })
    })
  },

  // 一键排课
  gotoSchedule() {
    const obj = {
      lessonType: this.data.isVip,
      idsStr: this.data.idsStr,
      className: this.data.name,
      classId: this.data.courseId
    }
    const params = JSON.stringify(obj);
    wx.navigateTo({
      url: `/pages/operation/class/classSchedule/index?params=${params}`,
    })
  },

  // 点击排课
  clickSchedule(e) {
    const obj = {
      id: e.currentTarget.dataset.query.id,
      lessonType: this.data.isVip,
      idsStr: this.data.idsStr,
      className: this.data.name,
      classId: this.data.courseId
    }
    const params = JSON.stringify(obj);
    wx.navigateTo({
      url: `/pages/operation/class/classSchedule/index?params=${params}`,
    })
  },

  // 获取学员信息
  async getStudentList() {
    classModel.getStudentListApi({ classId: this.data.courseId, pageNum: 1, pageSize: 999 }).then(res => {
      this.setData({
        studentTableDatas: res.rows
      })
    })
  },

  // 添加学员
  addStudent() {
    wx.navigateTo({
      url: `/pages/operation/class/addTempStudent/index?idsStr=${this.data.idsStr}&classIdM=${this.data.courseId}`,
    })
  },

  // 调至其他班
  removeOtherClass(e) {
    const val = e.currentTarget.dataset.query
    this.setData({
      removeId: val.id,
      removeStudentId: val.studentId
    })
    classModel.getClassListApi({ pageNum: 1, pageSize: 9999, isVip: this.data.isVip, courseId: val.courseId, beOver: 1 }).then(res => {

      let arr = [];
      res.rows.forEach(item => {
        if (item.id != this.data.classId) {
          arr.push(item)
        }
      })

      this.setData({
        showPicker: true,
        columnDatas: arr
      })
    })

  },
  onConfirmPicker(e) {
    const { value } = e.detail;
    this.setData({
      showPicker: false
    })
    Dialog.confirm({ title: "调至其他班？" }).then(() => {
      classModel.removeStudentApi({ classId: this.data.classId, id: this.data.removeId, studentId: this.data.removeStudentId, classIdNew: value.id }).then(res => {
        this.getStudentList()
      })
    })
  },
  onCancelPicker() {
    this.setData({
      showPicker: false
    })
  },

  // 移除本班
  removeClass(e) {
    const val = e.currentTarget.dataset.query;
    Dialog.confirm({ title: "移除本班？" }).then(() => {
      classModel.removeStudentApi({ classId: this.data.classId, id: val.id, studentId: val.studentId }).then(res => {
        this.getStudentList()
      })
    })
  },

  // 获取有点名的日期
  async getOrderDate() {
    classModel.getOrderDateListApi({ classId: this.data.courseId }).then(res => {
      this.setData({
        spotMap: res
      })
    })
  },

  // 根据日期获取点名数据
  getOrdeListByDate(date) {
    classModel.getOrdeListApi({ classDates: date, classId: this.data.courseId, pageNum: 1, pageSize: 999 }).then(res => {
      this.setData({
        orderTableDatas: res.rows
      })
    })
  },
  // 点名 日期改变的回调
  selectDay({ detail }) {
    this.setData({
      currentOrderDate: detail
    })
    this.getOrdeListByDate(detail)
  },

  // 调整课次及详情
  changeSchedule(e) {
    const item = e.currentTarget.dataset.query;
    if (item.button == '0' && this.data.beOver == '1') {
      // 调整课次
      this.setData({
        showSchedule: true,
        "scheduleObj.id": item.id,
        "scheduleObj.cofId": item.cofId,
        "scheduleObj.orderNo": item.orderNo,
        "scheduleObj.className": item.className,
        "scheduleObj.classDate": item.classDate,
        "scheduleObj.classStartTime": item.classStartTime,
        "scheduleObj.classEndTime": item.classEndTime,
        "scheduleObj.teacherName": item.teacherName,
        "scheduleObj.classRoomName": item.classRoomName,
        "scheduleObj.totalNum": item.totalNum
      })
    }
    else {
      // 查看详情

      classModel.getClassTimeStudentListApi({ pageNum: 1, pageSize: 999, orderNo: item.orderNo }).then(res => {
        this.setData({
          showOrderDetail: true,
          orderDetails: res.rows
        })
      })

    }
  },
  onClosePopup() {
    this.setData({
      showSchedule: false
    })
  },
  onCloseDetailPopup() {
    this.setData({
      showOrderDetail: false
    })
  },
  onRadioChange(e) {
    this.setData({
      "scheduleObj.type": e.detail
    })
  },
  // 确定调整课次
  confirmSchedule() {
    if (this.data.scheduleObj.type == '1') {
      // 只调整此次课程
      this.setData({
        showSchedule: false
      })
      wx.navigateTo({
        url: `/pages/operation/class/classCourseEdit/index?id=${this.data.scheduleObj.id}&orderNo=${this.data.scheduleObj.orderNo}`,
      })
    }
    else {
      // 调整此次课次及后续课次
      const obj = {
        id: this.data.scheduleObj.cofId,
        lessonType: this.data.isVip,
        idsStr: this.data.idsStr,
        className: this.data.name,
        classId: this.data.courseId
      }
      const params = JSON.stringify(obj);
      this.setData({
        showSchedule: false
      })
      wx.navigateTo({
        url: `/pages/operation/class/classSchedule/index?params=${params}`,
      })
    }
  },
  // 去点名
  gotoOrder(e) {
    wx.navigateTo({
      url: `/pages/operation/class/classOrder/index?id=${e.currentTarget.dataset.query.id}&orderNo=${e.currentTarget.dataset.query.orderNo}&idsStr=${e.currentTarget.dataset.query.courseIds}&classDate=${e.currentTarget.dataset.query.classDate}&cofId=${e.currentTarget.dataset.query.cofId}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  // 
  getTime() {
    let that = this;
    let myDate = new Date(); // 获取系统当前时间
    let sysmonth = myDate.getMonth() + 1;
    let nowDate = myDate.getDate() // 当前是本月几日
    let today = myDate.toLocaleDateString() // 今日年月日
    this.setData({
      nowDate: nowDate,
      sysmonth: sysmonth,
    })
  },

  // 获取日期详情


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})